package com.gopay.checkorder.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.checkorder.dao.TpasOrderDAO;
import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.bank.BankOrderLock;
import com.gopay.common.constants.bank.tpas.TpasChannelContants;
import com.gopay.common.constants.bank.tpas.TpasOrderStatus;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.bank.tpas.TpasOrder;
import com.gopay.common.util.DateUtils;

@Repository("tpasOrderDAO")
public class TpasOrderDAOImpl extends BaseDAOImpl<TpasOrder, String> implements TpasOrderDAO{

    @Override
    /**
     * 欧飞对单
     */
    public List<TpasOrder> getCheckWachgeOrder(int pageIndex, int pageSize) {
        StringBuffer hql = new StringBuffer();
        hql.append("from TpasOrder ");
        hql.append(" where orderStatus in (:oStatus)");
        hql.append(" and lock = :lock");            
        hql.append(" and prodCode in (:prodCode)");            
        hql.append(" and totalCheckTimes <= :totalCheckTimes");            
        Map<String, Object> params = new HashMap<String, Object>();
        String[] oStatus = {TpasOrderStatus.PROCESS,TpasOrderStatus.ENTER_ACCOUNT_FIAL,TpasOrderStatus.FAIL_ACCOUNT_FIAL};
        String[] prodCode = {IntTxnCd._01611.value,IntTxnCd._01612.value,IntTxnCd._01613.value,IntTxnCd._01618.value};//add zhoupeng 20170619 01618
        params.put("oStatus", oStatus);
        params.put("lock", 0);
        params.put("prodCode", prodCode);
        params.put("totalCheckTimes", 7);
        List<TpasOrder> list = this.findByHql(hql.toString(), params, pageIndex, pageSize);
        return list;
    }

    
    @Override
    public int getCheckWachgeOrderCount() {
        StringBuffer hql = new StringBuffer();
        hql.append("select count(*) from TpasOrder ");
        hql.append(" where orderStatus in (:oStatus)");
        hql.append(" and lock = :lock");            
        hql.append(" and prodCode in (:prodCode)");            
        hql.append(" and totalCheckTimes <= :totalCheckTimes");            
        Map<String, Object> params = new HashMap<String, Object>();
        String[] oStatus = {TpasOrderStatus.PROCESS,TpasOrderStatus.ENTER_ACCOUNT_FIAL,TpasOrderStatus.FAIL_ACCOUNT_FIAL};
        String[] prodCode = {IntTxnCd._01611.value,IntTxnCd._01612.value,IntTxnCd._01613.value,IntTxnCd._01618.value};//add zhoupeng 20170619 01618
        params.put("oStatus", oStatus);
        params.put("lock", 0);
        params.put("prodCode", prodCode);
        params.put("totalCheckTimes", 7);
        int rsCount = this.findCountByHql(hql.toString(), params);
        return rsCount;
    }
    
    @Override
    public int lock(TpasOrder order) {
        String currDate = DateUtils.format(DateUtils.getSystemDate());
        StringBuffer hql = new StringBuffer();
        hql.append("update TpasOrder set lock = :lock");
        hql.append(",lastUpdTs = to_date( :lastUpdTs ,'yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where outOrderId = :outOrderId");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("lock", BankOrderLock.LOCK);
        params.put("lastUpdTs", currDate);
        params.put("outOrderId", order.getOutOrderId());
        return this.updateByHql(hql.toString(), params);
    }

    @Override
    public int unlock(TpasOrder order) {
    	String currDate = DateUtils.format(DateUtils.getSystemDate());
        StringBuffer hql = new StringBuffer();
        hql.append("update TpasOrder set lock = :lock");
        hql.append(",lastUpdTs = to_date( :lastUpdTs ,'yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where outOrderId = :outOrderId");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("lock", BankOrderLock.UN_LOCK);
        params.put("lastUpdTs", currDate);
        params.put("outOrderId", order.getOutOrderId());
        return this.updateByHql(hql.toString(), params);
    }

	@Override
	public int updateInAcctFail(String orderid, String status) {
		String curDate = DateUtils.getCurrDate();

        StringBuffer hql = new StringBuffer();
        hql.append("update TpasOrder set orderStatus = '" + status + "',");
        hql.append("lastUpdTs = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderid + "'");

        return this.bulkUpdate(hql.toString());
	}


    
}
